<?php
require_once 'lib/controllerWithoutGetOrPost.php';
require_once 'view/standard-top.php';

function pageNavLink($totalpages, $pagenumber, $currentpage, $firstname, $surname) {
	if($pagenumber <= $totalpages) {
		if($currentpage != $pagenumber) {
	 		return "<a href=\"searchpeople.php?surname=".$surname."&firstname=".$firstname."&page=".$pagenumber."\">".$pagenumber."</a>\n\t";	
		} else {
			return "<strong>[ ".$pagenumber." ]</strong>\n\t";
		}
	}
}

function insertSearchPageTopNavigation($surname, $firstname, $totalpages, $currentpage) {

$startpage = $currentpage;
if($currentpage < 3) {
	$startpage = 3;
}

$page1 = $startpage-2;
$page2 = $startpage-1;
$page3 = $startpage;
$page4 = $startpage+1;
$page5 = $startpage+2;

echo "<li class=\"sortby\">";
echo "<a href=\"searchpeople.php?surname=".$surname."&firstname=".$firstname."&page=1\">&lt;&lt;</a>\n\t";
echo pageNavLink($totalpages, $page1, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page2, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page3, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page4, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page5, $currentpage, $firstname, $surname);
echo "...\n\t";
echo "<a href=\"searchpeople.php?surname=".$surname."&firstname=".$firstname."&page=".$totalpages."\">".$totalpages."</a>\n\t";
echo "<a href=\"searchpeople.php?surname=".$surname."&firstname=".$firstname."&page=".$totalpages."\">&gt;&gt;</a>\n\t";
echo "</li>";
						
	
}

function insertSearchPageBottomNavigation($surname, $firstname, $totalpages, $currentpage) {

$startpage = $currentpage;
if($currentpage < 3) {
	$startpage = 3;
}

$page1 = $startpage-2;
$page2 = $startpage-1;
$page3 = $startpage;
$page4 = $startpage+1;
$page5 = $startpage+2;

echo "<li class=\"sortby\">";
echo "<a href=\"#topbar\" title=\"Back to Top\" class=\"leftfloat\">Back to Top</a>\n\t";
echo "<a href=\"searchpeople.php?surname=".$surname."&firstname=".$firstname."&page=1\">&lt;&lt;</a>\n\t";
echo pageNavLink($totalpages, $page1, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page2, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page3, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page4, $currentpage, $firstname, $surname);
echo pageNavLink($totalpages, $page5, $currentpage, $firstname, $surname);
echo "...\n\t";
echo "<a href=\"searchpeople.php?surname=".$surname."&firstname=".$firstname."&page=".$totalpages."\">".$totalpages."</a>\n\t";
echo "<a href=\"searchpeople.php?surname=".$surname."&firstname=".$firstname."&page=".$totalpages."\">&gt;&gt;</a>\n\t";
echo "</li>";
					
						
	
}

$firstname = trim($_GET['firstname']);
$surname = trim($_GET['surname']);

if($firstname == "First Name") {
	$firstname = "";
}

$query = <<< EOT
    SELECT
        Person.ID, 
        Person.Surname, 
        Person.FirstNames, 
        Person.FormerName, 
        Person.NeeName, 
        Person.AlsoKnownAs, 
        Person.DeathDate, 
        Person.BirthDate, 
        Person.Age AS 'Age', 
        Person.AgeMeasure AS 'AgeMeasure', 
        CityTown.Name as 'CityTown', 
        c2.Name as 'FormerCityTown', 
        Region.Name AS 'Region', 
        Country.Name AS 'Country', 
        Charity.Name AS 'Charity', 
        FuneralHome.Name AS 'FuneralHome',
        Person.OtherInfo As 'Other Information'
    FROM Person 
        LEFT JOIN Region
            ON Person.RegionID = Region.ID 
        LEFT JOIN Country
            ON Country.ID = Person.CountryID 
        LEFT JOIN CityTown AS c2
            ON Person.FormerCityTownID = c2.ID 
        LEFT JOIN Charity
            ON Charity.ID = Person.CharityID 
        LEFT JOIN FuneralHome
            ON FuneralHome.ID = Person.FuneralHomeID 
        LEFT JOIN CityTown
            ON CityTown.ID = Person.CityTownID
EOT;
$blah = "WHERE (Person.Surname Like '".mysql_real_escape_string($surname)."%' OR Person.NeeName Like '".mysql_real_escape_string($surname)."%' OR Person.FormerName Like '".mysql_real_escape_string($surname)."%') AND (Person.FirstNames Like '".mysql_real_escape_string($firstname)."%' OR Person.AlsoKnownAs Like '".mysql_real_escape_string($firstname)."%') ORDER BY Person.DeathDate DESC LIMIT ";
$querycount = "select count(*) from Person WHERE (Person.Surname Like '".mysql_real_escape_string($surname)."%' OR Person.NeeName Like '".mysql_real_escape_string($surname)."%' OR Person.FormerName Like '".mysql_real_escape_string($surname)."%') AND (Person.FirstNames Like '".mysql_real_escape_string($firstname)."%' OR Person.AlsoKnownAs Like '".mysql_real_escape_string($firstname)."%')";
$resultcount = mysql_query($querycount, $link);

if (!$resultcount) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

$resultcount = mysql_result($resultcount, 0);

$totalpages = 1;
$resultsperpage = 10;

$totalpages = ceil($resultcount/$resultsperpage);

if($totalpages < 1) {
	$totalpages = 1;
}

$from = 0;
$to = $resultsperpage;
$currentpage = 1;

$pagenumber = intval($_GET['page']);
if(!empty($pagenumber)) {
	$currentpage = $pagenumber;
	$to = $currentpage * $resultsperpage;
	$from = $to - $resultsperpage;
	$blah = $blah.$from.",".$resultsperpage;	

} else {
	$blah = $blah."0,".$resultsperpage;
}

if(empty($pagenumber)) {
	if($pagenumber < $totalpages) {
		$pagenumber += 1;
	}	
} else {
	$pagenumber += 2;
}



$query .= ' '.$blah;

//echo $query;

// Perform Query
$result = mysql_query($query, $link);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}


$class1 = "odd";
$class2 = "even";
$class = $class1;

function showage($age) {
	if($age > 0) {
		return "Age ".$age;
	}
}

function formatdatetime($date) {
	if($date == "0000-00-00 00:00:00") {
		return "";
	}   else {
		return date('d F Y', strtotime(str_replace("&", "&amp;", $date)));
	}
}


function showbirthdate($birthdate) {
	if(strlen($birthdate) > 0) {
		return " <strong>b.</strong> ".$birthdate;
	}
}

function showdeathdate($deathdate) {
	if(strlen($deathdate) > 0) {
		return "Died ".$deathdate.". ";
	}
}


function showformerly($former) {
	if(strlen($former) > 0) {
		return "<strong>".$former." </strong>"; 
	}

}

function showaka($aka) {
	if(strlen($aka) > 0) {
		return "Also known as <b> ".$aka."</b>."; 
	}
	
}

function shownee($nee) {
	if(strlen($nee) > 0) {
		return "nee <strong>".$nee."</strong>";
	}
}

function showageextra($age, $extra) {
	if(strlen(showage($age))> 0) {
		return $extra;
	}

}

function showneeandformer($nee, $former) {
  if(strlen(shownee($nee)) > 0 || strlen(showformerly($former)) > 0) {
    return "<p>Other Name(s): ".showformerly($former).shownee($nee).".</p>";
  } else {
    return "";
  }
}

function showfirstname($first) {
	if(strlen($first) > 0) {
		return $first." ";
	}
}

echo "<ul id=\"searchresults\">";

echo "<li class=\"header\"><h2>Search Results for ".showfirstname($firstname).$surname."</h2></li>";


if(!empty($surname)) { 

  echo "<li class=\"resultinfo\"><span>(".$resultcount.") listing results &nbsp; (".$resultsperpage.") results per page &nbsp; (".$totalpages.") pages </span><a href=\"search_tips.php\">Search Tips</a></li>";

  //this <a href=\"#\" title=\"\">Search Tips</a> goes between the above </span> and </li>...
  insertSearchPageTopNavigation(trim($_GET['surname']), trim($_GET['firstname']), $totalpages, $currentpage);

  


  if(mysql_num_rows($result)!=0) {
    echo "<li><ul>";
    while ($row = mysql_fetch_assoc($result)) {

      echo "<li>";



     //echo .showbirthdate(formatdatetime($row['BirthDate'])).."<br /><br />";


      echo "<p class=\"results_name\"><a href=\"remembrance.php?personid=".$row['ID']."\" title=\"\" class=\"mainname\">".strtoupper($row['Surname']).", ".$row['FirstNames']."</a>. ";
      echo showaka($row['AlsoKnownAs'])."</p>";
      echo showneeandformer($row['NeeName'], $row['FormerName']);
      echo "<p>".showdeathdate(formatdatetime($row['DeathDate'])).showage($row['Age']).showageextra($row['Age'],$row['AgeMeasure'])."</p>";
					
      echo "<p>Remembrance Page Open <a href=\"remembrance.php?personid=".$row['ID']."\">sign page or view entries</a></p>";
					
			$query2 = "select count(*) from Publication left join PublicationPerson on Publication.ID = PublicationPerson.PublicationID where PublicationPerson.PersonID = ".$row['ID'];
      $result2 = mysql_query($query2, $link);
	    $counter = 0;

	    if(mysql_num_rows($result2)!=0) {
	      //echo "<strong>Public Notices on File</strong> "; 
        echo "<p class=\"left\">Published Notices (".mysql_result($result2, 0).") <a href=\"personnotices.php?personid=".$row['ID']."\" title=\"view notices\">view notices</a></p>";
      } else {
        echo "<p class=\"left\">Published Notices (0) <a href=\"personnotices.php?personid=".$row['ID']."\" title=\"view notices\">view notices</a></p>";
      }
	    //echo "</div>";
      //echo "<p>Remembrance Page Open <a href=\"#\" title=\"sign page\">sign page</a> <a href=\"#\" title=\"view entries\">view entries</a></p>";
      echo "<p class=\"right\">".$row['Country']."&nbsp</p>";
      echo "</li>";
    } //ended while loop..
    echo "</ul></li>";
    insertSearchPageBottomNavigation(trim($_GET['surname']), trim($_GET['firstname']), $totalpages, $currentpage);
    echo "</ul>";
  } else {
echo "</ul>";
echo "<ul id=\"centerbar\"><li><p>Sorry but we have no matching name for the name you have entered.</p>";

echo "<p>Please check your spelling or try alternate spelling of the names.</p>";

echo "<p>It may also be possible that no public notice has been received or we have not yet processed* the notice.  Please try again later.</p>";

echo "<p>*receiving and processing notices can take up to 7 days from print date.</p></li></ul>";

  }
  

}

require_once 'view/standard-bottom.php';
?>
